package com.fairytask.tools
{
	import com.fairytask.data.Modulator;
	import com.fairytask.engine.utils.IO;
	import com.fairytask.event.SystemTimeEvent;
	

	/**
	 * 服务器系统时间 
	 * @author hanjy
	 * 
	 */	
	public class SystemTime
	{
		public static var SERVERTIME	:int;//服务器时间
		
		private static var _instance	:SystemTime;
		private static var _diff		:Number=0;
		
		//变量
		private var _model	:Modulator;
		/**
		 * 取得单例 
		 * @return 
		 * 
		 */
		public static function getInstance():SystemTime
		{
			if(null==_instance)
			{
				_instance	=	new SystemTime();
			}
			return _instance;
		}
		
		public function SystemTime()
		{
			_model	=	Modulator.instance;
			//初始化时间，以本地时间为准
			SERVERTIME	=	new Date().getTime();
			//IO.traceLog("本地当前时间为:",SERVERTIME);
			//添加事件
			addEvent();
		}
		
		/**
		 * 事件监听 
		 */
		private function addEvent():void
		{
			_model.addEventListener(SystemTimeEvent.SYSTEM_TIME, onSystemTimer);
		}
		
		/**
		 * 获得系统时间。 
		 * int
		 * @param evt
		 * 状态开始时间, 持续时间
		 */
		private function onSystemTimer(evt:SystemTimeEvent):void
		{
			var serverTime	:Number	=	evt.time*1000;
			var localTime	:Number	=	new Date().getTime();
			IO.traceLog("服务器时间为(ms):",serverTime);
			IO.traceLog("本地当前时间为(ms):",new Date().getTime());
			_diff	=	serverTime-localTime;
		}
		
		/**
		 * 取得状态剩余时间 
		 * @param fTime 开始时间
		 * @param cTime 持续时间
		 * @author hanjy
		 * 	return <b>返回毫秒单位</b>
		 */   
		public static function getLeftTime(fTime:Number,cTime:Number):Number
		{
			var serverNow	:Number	=	new Date().getTime()+_diff;	
			var tillTime	:Number	=	fTime+cTime;
			if(serverNow>tillTime)
			{
				return 0;
			}
			else
			{
				return tillTime-serverNow;
			}
		}
		
		public static function get diff():Number
		{
			return _diff;
		}
		
		/**
		 * 取得天、时、分、秒 
		 * @param pInt 毫秒数
		 * @param pFormat
		 * @return 
		 * 
		 */
		public static function retrieveFormatByInt(pInt:Number,pFormat:String=""):String
		{
			var str	:String	=	"";
			var day		:int	=	0;
			var hour	:int	=	0;
			var minit	:int	=	0;
			var second	:int	=	0;
			if(pFormat=="")
			{
				day	=	pInt/86400000;
				pInt=	pInt%86400000;
				hour=	pInt/3600000;
				pInt=	pInt%3600000;
				minit = pInt/60000;
				pInt=	pInt%60000;
				second=	pInt/1000;
				if(day>0)
				{
					str	+= day + "天";
					str	+= hour + "小时";
					str	+= minit + "分钟";
					str	+= second + "秒";
				}
				else
				{
					if(hour>0)
					{
						str	+= hour + "小时";
						str	+= minit + "分钟";
						str	+= second + "秒";
					}
					else
					{
						str	+= minit + "分钟";
						str	+= second + "秒";
					}
				}
			}
			return str;
		}
		
		
		/**
		 *	获取天数
		 * @param pInt		秒数
		 * @return 
		 * 
		 */		
		public static function getDays(pInt:Number):String
		{
			var date	:Date=new Date(pInt*1000);
			return date.day+"天";
		}
		
		public static function getLeaveDays(pInt:Number):int
		{
			var localTime	:Number	=	new Date().getTime();
			var serverTime	:Number	=	localTime+_diff;
			var leaveDay	:int =	(serverTime-pInt*1000)/86400000;
			return leaveDay;
		}
		
		/**
		 *		获取分钟：描述 
		 * @param second
		 * 
		 */		
		public static function getMinSec(second:int):String
		{
			var date	:Date	=	new Date(second*1000);
			
			var min		:String;
			var sec			:String;
			if(date.minutes<10)
				min	=	"0"+date.minutes;
			else
				min	=	String(date.minutes);
			
			if(date.seconds<10)
				sec	=	"0"+date.seconds;
			else
				sec	=	String(date.seconds);
			
			return min+":"+sec;
		}
	}
}